




(function($) {
	Date.prototype.getWeek = function() {
		var onejan = new Date(this.getFullYear(),0,1);
		return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
	}
	$.kulstats.months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var d = new Date();
	$.kulstats.current = {week: d.getWeek(), month: d.getMonth(), semester: ((d.getMonth() > 1 && d.getMonth() < 8) ? 2 : 1)};
	// stat variables
	$.kulstats.stats = {};
})(jQuery);

$('#statsPage').live('pageinit',function(e) {
	
	// initializing local variables
	//$.kulstats.stats.chart = $("#mainChart");
	//$.kulstats.stats.chart = document.getElementById("mainChart");
	$.kulstats.stats.chartDivs = new Array();
	$.kulstats.stats.charts  = $("#stats_all_charts");
	$.kulstats.stats.loader  = $("#stats_loading");
	
	// set initial date:
	$("#stats_selection_offs_curr").text("Week " + $.kulstats.current.week);

	$("[name^=stats_selection_]").change(function(event) {
		var stat_type = $('[name=stats_selection_type]:checked').val(); // time_spent / time_courses / time_spare / prod_total
		var stat_whom = $('[name=stats_selection_whom]:checked').val(); // personal / global
		var stat_time = $('[name=stats_selection_time]:checked').val(); // week / month / semester
		var stat_offs = $("#stats_selection_offs").val();
		var stat_char = $('[name=stats_selection_char]:checked').val(); // chart_pie / chart_bar
		var stat_crse = $('[name=stats_selection_crse]').val(); // pie / bar
		
		// adjust possible input fields:
		// - course selection
		if(stat_type == "time_courses" && stat_char == "chart_bar") {
			$("#stats_selection_crse_div").show();
		}
		else {
			$("#stats_selection_crse_div").hide();
			stat_crse = "";
		}

		var chartId = "stats_chart_"+stat_type+"_"+stat_whom+"_"+stat_time+"_"+stat_offs+"_"+stat_char+"_"+stat_crse;
		// hide all charts
		$("[id^=stats_chart_]").hide();
		$.kulstats.stats.loader.show();
		// show wanted chart
		if($.kulstats.stats.chartDivs[chartId]) {
			$.kulstats.stats.chartDivs[chartId].show();
			$.kulstats.stats.loader.hide();
		}
		else {
			var query = new google.visualization.Query('/ChartData?stat_type='+stat_type+'&stat_whom='+stat_whom+'&stat_time='+stat_time+'&stat_offs='+stat_offs+'&stat_char='+stat_char+'&stat_crse='+stat_crse);
			query.send(function (response) {
				//received data:
				var data = response.getDataTable();
				//create the chart division tag
				var winWidth = $(window).width();
				$.kulstats.stats.charts.append(
					$("<div/>")
						.attr("id",chartId)
						.css("display","none")
						.css("width",winWidth)
						.css("text-align","center")
				);
				
				//create the chart depending on the chart type needed
				if(data.G.length > 0) {
					
					if(stat_char == "chart_pie") {
						var options = {cht: "p", chf: "bg,s,00000000", width: (0.9*winWidth) , chco :"3399cc,bbcced,ffcc33,ff2828,990066,ff9900,00ffd2,ff00a2,f6f427,80c65a,ff0000", height: 500, title: ''};
						var chart = new google.visualization.ImageChart(document.getElementById(chartId));
					}
					else if(stat_char == "chart_bar") {
						var options = {cht: "bvg", chf: "bg,s,00000000", width: (0.9*winWidth), title: ''};
						var chart = new google.visualization.ImageChart(document.getElementById(chartId));
					}
					chart.draw(data, options);
				}
				else {
					$("#"+chartId).html("No data available, go <a href=\"/track\">track</a> some stuff!");
				}
				//showing the chart
				$.kulstats.stats.chartDivs[chartId] = $("#"+chartId);
				$.kulstats.stats.loader.hide();
				$.kulstats.stats.chartDivs[chartId].show();
			});
		}
	});
	
	
	$('[name=stats_selection_time]').change(function(event) {
		var type = $('[name=stats_selection_time]:checked').val();
		if(type == "semester") {
			$("#stats_selection_offs_curr").text("Semester " + $.kulstats.current.semester);
		}
		else if(type == "month") {
			$("#stats_selection_offs_curr").text($.kulstats.months[$.kulstats.current.month]);
		}
		else {
			$("#stats_selection_offs_curr").text("Week " + $.kulstats.current.week);
		}
		$("#stats_selection_offs").val(0);
	});
	$('[name=stats_selection_type]').change(function(event) {
		var type = $('[name=stats_selection_type]:checked').val();
		if(type == "prod_total") {
			$("#stats_selection_char_pie").prop("disabled",true).prop("checked",false).checkboxradio("refresh");
			$("#stats_selection_char_bar").prop("checked",true).checkboxradio("refresh");
			$("[name=stats_selection_char]").change();
		}
		else {
			$("#stats_selection_char_pie").prop("disabled",false).checkboxradio("refresh");
			$("#stats_selection_char_bar").prop("disabled",false).checkboxradio("refresh");
			$("[name=stats_selection_char]").change();
		}
	});
	
	
	$("#stats_selection_offs_back").click(function(event) {
		var current = parseInt($("#stats_selection_offs").val());
		if($('[name=stats_selection_time]:checked').val() == "week") {
			if(current <= 50) {
				$("#stats_selection_offs_curr").text("Week " + ($.kulstats.current.week -current-1));
				$("#stats_selection_offs").val((current+1));
				$("#stats_selection_offs").change();
			}
		}
		else if($('[name=stats_selection_time]:checked').val() == "month") {
			if(current <= 11) {
				$("#stats_selection_offs_curr").text($.kulstats.months[($.kulstats.current.month - current - 1)]);
				$("#stats_selection_offs").val((current+1));
				$("#stats_selection_offs").change();
			}
		}
		else if($('[name=stats_selection_time]:checked').val() == "semester") {
			if(current == 0 && $.kulstats.current.semester == 2) {
				$("#stats_selection_offs_curr").text("Semester 1");
				$("#stats_selection_offs").val((current+1));
				$("#stats_selection_offs").change();
			}
		}
	});
	$("#stats_selection_offs_forw").click(function(event) {
		var current = parseInt($("#stats_selection_offs").val());
		if(current > 0) {
			if($('[name=stats_selection_time]:checked').val() == "week") {
				$("#stats_selection_offs_curr").text("Week " + ($.kulstats.current.week -  current + 1));
				$("#stats_selection_offs").val((current-1));
				$("#stats_selection_offs").change();
			}
			else if($('[name=stats_selection_time]:checked').val() == "month") {
				$("#stats_selection_offs_curr").text($.kulstats.months[($.kulstats.current.month - current + 1)]);
				$("#stats_selection_offs").val((current-1));
				$("#stats_selection_offs").change();
			}
			else if($('[name=stats_selection_time]:checked').val() == "semester") {
				$("#stats_selection_offs_curr").text("Semester 2");
				$("#stats_selection_offs").val((current-1));
				$("#stats_selection_offs").change();
			}
		}
	});

	$('[name=stats_selection_type]').change(function(event) {
		var type = $('[name=stats_selection_time]:checked').val();
		if(type == "semester") {
			$("#stats_selection_offs_cur").text("Semester " + $.kulstats.current.semester);
		}
		else if(type == "month") {
			$("#stats_selection_offs_cur").text($.kulstats.months[$.kulstats.current.month]);
		}
		else {
			$("#stats_selection_offs_cur").text("Week " + $.kulstats.current.week);
		}
		$("#stats_selection_offs").val(0);
	});
	
	
	
	/* OLD METHODS
	// load the google chart api
	google.setOnLoadCallback(function () {
		//alert("2");
		
		var query = new google.visualization.Query('/ChartData?stat_type=blabla');
		query.send(function (response) {
			var data = response.getDataTable();
			var chart = new google.visualization.ColumnChart($.kulstats.stats.chart);
	    	//chart.draw(data, {width: 600, height: 240, title: 'Hours by weekday',
			//	hAxis: {title: 'Day', titleTextStyle: {color: 'black'}}});
			chart.draw(data, { title: 'Hours by weekday',
					hAxis: {title: 'Day', titleTextStyle: {color: 'black'}}});
			//bgcolor: #F0F0F0
		});
	});
	
	$("#stats_button_1").click(function(event) {
		var query = new google.visualization.Query('/ChartData?stat_type=stat_time_spend');
		query.send(function (response) {
			var data = response.getDataTable();
			var options = {cht: "p", chf: "bg,s,00000000", title: 'Time Spend'};
			var chart = new google.visualization.ImageChart($.kulstats.stats.chart);
	    	//chart.draw(data, {width: 600, height: 240, title: 'Hours by weekday',
			//	hAxis: {title: 'Day', titleTextStyle: {color: 'black'}}});
			chart.draw(data, options); //{ title: 'Time Spend', backgroundColor: "E4E4E4"});
			//alert(response);
		});
	});
	$("#stats_button_2").click(function(event) {
		var query = new google.visualization.Query('/ChartData?stat_type=highscores_points');
		query.send(function (response) {
			var data = response.getDataTable();
			var options = {cht: "bhs", chf: "bg,s,00000000", title: 'Highscores points', legend: 'none'};
			var chart = new google.visualization.ImageChart($.kulstats.stats.chart);
	    	//chart.draw(data, {width: 600, height: 240, title: 'Hours by weekday',
			//	hAxis: {title: 'Day', titleTextStyle: {color: 'black'}}});
			chart.draw(data, options); //{ title: 'Time Spend', backgroundColor: "E4E4E4"});
			//alert(response);
		});
	});
	
	*/
	
});








